package com.leon.lifeenginespace;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Locale;
import java.util.Map;


/**
 * ApplicationContext 是 BeanFactory 的组合
 * BeanFactory 是 ApplicationContext 的成员变量t
 */
//@SpringBootApplication
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, RedisAutoConfiguration.class})//启动不需要数据库
public class LifeEngineSpaceApplication {

    public static void main(String[] args) throws Exception {
        //spring容器 ConfigurableApplicationContext
        /*
         */
        ConfigurableApplicationContext run = SpringApplication.run(LifeEngineSpaceApplication.class, args);

        ConfigurableListableBeanFactory beanFactory = run.getBeanFactory();
        //Field用于获取某个类的属性或该属性的属性值
        Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
        //Accessible为true 访问私有属性
        singletonObjects.setAccessible(true);
        Map<String, Object> o = (Map<String, Object>) singletonObjects.get(beanFactory);
        o.forEach((k, v) -> {
            System.out.println(k + "=" + v);
        });
        //messageSource(国际化 翻译) 通过请求头
        System.out.println(run.getMessage("hi", null, Locale.CHINA));

        Resource[] resources = run.getResources("classpath:application.properties");
        for (Resource resource : resources) {
            System.out.println(resource);
        }
    }
}
